package com.stars.empoly.activity

import android.app.Activity
import android.content.Intent
import android.os.Bundle
import androidx.core.net.toUri
import coil.load
import com.baidu.mapapi.search.core.PoiInfo
import com.google.gson.Gson
import com.luck.picture.lib.PictureSelector
import com.qmuiteam.qmui.kotlin.onClick
import com.stars.empoly.base.MBActivity
import com.stars.empoly.config.MIntentAction
import com.stars.empoly.databinding.ActivityPersonalInfoBinding
import com.stars.empoly.inte.ChoiceData
import com.stars.empoly.inte.OnUploadCallback
import com.stars.empoly.model.bean.UpImgBean
import com.stars.empoly.utils.FileUtils
import com.stars.empoly.utils.OssUtils
import com.stars.empoly.utils.PopuListUtils
import com.stars.empoly.vmodel.PersonalInfoVModel
import java.io.File
import java.util.ArrayList

/**
 *on 2021/5/16
 */
class PersonalInfoActivity : MBActivity<PersonalInfoVModel, ActivityPersonalInfoBinding>(), OnUploadCallback<UpImgBean> {

    override fun init(savedInstanceState: Bundle?) {
        super.init(savedInstanceState)
        vm.setInitData()
    }


    override fun listeners() {
        super.listeners()
        binding.llSubmit.onClick {
            vm.toSubmit()
        }
        binding.llSex.onClick {
            var list=ArrayList<String>()
            list.add("男")
            list.add("女")
            PopuListUtils.showQMUIPopup(context(),binding.tvSex,list,object :ChoiceData{
                override fun onRestuse(context: String?) {
                    binding.tvSex.text = context
                }

            })
        }
    }


    override fun onActivityResult(requestCode: Int, resultCode: Int, data: Intent?) {
        super.onActivityResult(requestCode, resultCode, data)
        if (resultCode != Activity.RESULT_OK) {
            return
        }
        if (requestCode == MIntentAction.REQUEST_CODE_ONE) {
            val bundle = data!!.extras
            if (null != bundle) {
                var data=bundle.getString("bean")
                val item: PoiInfo = Gson().fromJson(data, PoiInfo::class.java)
                binding.tvAddress.text = item.city+"-"+item.address

            }


            return
        }




        if (requestCode == MIntentAction.REQUEST_CODE_THREE) {//图片

            val selectList = PictureSelector.obtainMultipleResult(data)
            val list = ArrayList<String>()
            if (null != selectList) {
//                for (path in selectList) {
//                    list.add(path.path)
//                }
                if(selectList[0].path.contains("content://")){
                    val cr = this.contentResolver
                    list.add( FileUtils.getFilePathFromContentUri(selectList[0].path.toUri(),cr))
                }else{
                    list.add(selectList[0].path)
                }


                binding.imgAvater.load(File(list[0]))

                // vm.headImg.value=list[0]
                OssUtils(this, vm)
                        .setUpLoad("avatar", list, this)

            }

            return

        }
        if (requestCode == MIntentAction.REQUEST_CODE_CAMERA) {//拍照
            //拍照
            val selectList = PictureSelector.obtainMultipleResult(data) ?: return
            val list = ArrayList<String>()
            if(selectList[0].path.contains("content://")){

                val cr = this.contentResolver
                list.add( FileUtils.getFilePathFromContentUri(selectList[0].path.toUri(),cr))

            }else{
                list.add(selectList[0].path)
            }

            binding.imgAvater.load(File(list[0]))

            // vm.headImg.value=list[0]
            OssUtils(this, vm)
                    .setUpLoad("avatar", list, this)

            return
        }


    }



    override fun vmClass(): Class<PersonalInfoVModel> {
        return PersonalInfoVModel::class.java
    }
    override fun binding(): ActivityPersonalInfoBinding {
       return ActivityPersonalInfoBinding.inflate(layoutInflater)
    }

    override fun title(): String? {
        return "个人信息"
    }

    override fun onUploadSuccess(t: UpImgBean) {

        vm.avatar = t.urlPath


    }

    override fun onUploadError(error: String?) {

    }
}
